package com.ljkj.cordial.chat.adapter;

import android.graphics.PorterDuff;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.bean.PayLoad;
import com.ljkj.cordial.chat.dao.AlbumTableBean;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.ljkj.cordial.chat.presenter.AChatPresenter;
import com.ljkj.cordial.chat.presenter.OnChatViewItemClickListener;
import com.ljkj.cordial.chat.util.AlbumManageUtil;
import com.ljkj.cordial.chat.widget.VideoPreviewDialog;
import com.nic.view.util.JLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 视频图片展示
 */
public class ChatItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public final static int PIC = 0;
    public final static int VIDEO = 1;
    private List<ChatTableBean> list;


    public void setList(List<ChatTableBean> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.list = list;
        notifyDataSetChanged();
    }

    public void add(ChatTableBean table) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.list.add(table);
        notifyDataSetChanged();
    }

    public void addOrReplace(ChatTableBean table) {
        if (list == null) {
            list = new ArrayList<>();
        }
        if (this.list.contains(table)) {
            this.list.remove(table);
        }
        this.list.add(table);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == PIC) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_item_picture, null);
            return new ViewPicHolder(view);
        } else {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_item_video, null);
            return new ViewVideoHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        int type = getItemViewType(i);
        ChatTableBean item = list.get(i);
        if (type == PIC) {
            ViewPicHolder viewPicHolder = (ViewPicHolder) holder;
            if (item.getSendState() == ChatTableBean.SEND_ING) {
                viewPicHolder.chatItemProgress.show();
            } else if (item.getSendState() == ChatTableBean.SEND_SUCCESS) {
                viewPicHolder.chatItemProgress.hide();
                viewPicHolder.ivChatItemFail.setVisibility(View.GONE);
                viewPicHolder.ivMsg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showLargePhoto(viewPicHolder, v, item);
                    }
                });

            } else if (item.getSendState() == ChatTableBean.SEND_FAIL) {
                viewPicHolder.chatItemProgress.hide();
                viewPicHolder.ivChatItemFail.setVisibility(View.VISIBLE);
                viewPicHolder.ivChatItemFail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onChatViewItemClickListener != null) {
                            onChatViewItemClickListener.onItemReloadClick(v, item, i);
                        }
                    }
                });
            }
            viewPicHolder.ivMsg.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (onChatViewItemClickListener != null) {
                        onChatViewItemClickListener.onItemLongClick(v, item, i);
                        return true;
                    }
                    return false;
                }
            });
        } else {
            ViewVideoHolder viewVideoHolder = (ViewVideoHolder) holder;
            if (item.getSendState() == ChatTableBean.SEND_ING) {
                viewVideoHolder.chatItemProgress.show();
            } else if (item.getSendState() == ChatTableBean.SEND_SUCCESS) {
                viewVideoHolder.chatItemProgress.hide();
                viewVideoHolder.ivVideoPlay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showVideo(viewVideoHolder, v, item);
                    }
                });
                viewVideoHolder.ivVideoFrame.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if (onChatViewItemClickListener != null) {
                            onChatViewItemClickListener.onItemLongClick(v, item, i);
                            return true;
                        }
                        return false;
                    }
                });
            } else if (item.getSendState() == ChatTableBean.SEND_FAIL) {
                viewVideoHolder.chatItemProgress.hide();
                viewVideoHolder.ivChatItemFail.setVisibility(View.VISIBLE);
                viewVideoHolder.ivChatItemFail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onChatViewItemClickListener != null) {
                            onChatViewItemClickListener.onItemReloadClick(v, item, i);
                        }
                    }
                });
            }


        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (payloads != null && !payloads.isEmpty()) {
            PayLoad payLoad = (PayLoad) payloads.get(0);
            ChatTableBean item = list.get(position);
            for (int type : payLoad.getType()) {
                if (PayLoad.ITEM_PIC == type) {
                    ViewPicHolder viewPicHolder = (ViewPicHolder) holder;
//                    AChatPresenter.showThumbPicture(item, viewPicHolder.ivMsg);
                }
                if (PayLoad.ITEM_VID == type) {
                    ViewVideoHolder viewVideoHolder = (ViewVideoHolder) holder;
//                    AChatPresenter.showThumbPicture(item, viewVideoHolder.ivVideoFrame);
                }
            }
        } else {
            onBindViewHolder(holder, position);
        }
    }

    private void showLargePhoto(ViewPicHolder viewPicHolder, View v, ChatTableBean item) {
        if (!TextUtils.isEmpty(item.getAlbumId()) && Long.parseLong(item.getAlbumId()) > 0) {
            AlbumTableBean al = AlbumTableBean.queryById(Long.parseLong(item.getAlbumId()));
            if (al != null && !TextUtils.isEmpty(al.getMPath())) {
                AlbumManageUtil
                        .getInstance().lookLargePhoto(v.getContext(), al.getMPath());
            }

        } else {
            if (viewPicHolder.chatItemProgress.isShown()) {
                return;
            }
            viewPicHolder.chatItemProgress.show();
            AlbumManageUtil.getInstance().downLoadFileFriend(item.getFileUrl(), AlbumTableBean.TYPE_IMAGE, item.getTargetId(), new AlbumManageUtil.OnFileResponse() {
                @Override
                public void onResult(String path, String fileId) {
                    viewPicHolder.chatItemProgress.hide();
                    JLog.d("pic->" + path);
                    if (!TextUtils.isEmpty(fileId) && Long.parseLong(fileId) > 0) {
                        item.setAlbumId(fileId);
                        ChatTableBean.update(item);
                        AlbumTableBean al = AlbumTableBean.queryById(Long.parseLong(fileId));
                        if (al != null && !TextUtils.isEmpty(al.getMPath())) {
                            AlbumManageUtil
                                    .getInstance().lookLargePhoto(v.getContext(), al.getMPath());
                        }
                    }


                }
            });
        }

    }

    private void showVideo(ViewVideoHolder viewVideoHolder, View v, ChatTableBean item) {
        if (!TextUtils.isEmpty(item.getAlbumId()) && Long.parseLong(item.getAlbumId()) > 0) {
            AlbumTableBean al = AlbumTableBean.queryById(Long.parseLong(item.getAlbumId()));
            if (al != null && !TextUtils.isEmpty(al.getMPath())) {
                new VideoPreviewDialog(v.getContext()).show(al.getMPath(),al.getMThumbPath());
            }

        } else {
            if (viewVideoHolder.chatItemProgress.isShown()) {
                return;
            }
            viewVideoHolder.chatItemProgress.show();
            AlbumManageUtil.getInstance().downLoadFileFriend(item.getFileUrl(), AlbumTableBean.TYPE_VIDEO, item.getTargetId(), new AlbumManageUtil.OnFileResponse() {
                @Override
                public void onResult(String path, String fileId) {
                    viewVideoHolder.chatItemProgress.hide();
                    JLog.d("vid->" + path);
                    if (!TextUtils.isEmpty(fileId) && Long.parseLong(fileId) > 0) {
                        item.setAlbumId(fileId);
                        ChatTableBean.update(item);
                        AlbumTableBean al = AlbumTableBean.queryById(Long.parseLong(fileId));
                        if (al != null && !TextUtils.isEmpty(al.getMPath())) {
                            new VideoPreviewDialog(v.getContext()).show(al.getMPath(),al.getMThumbPath());
                        }
                    }


                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        ChatTableBean item = list.get(position);
        if (item.getChatType() == MsgType.Video) {
            return VIDEO;
        }
        return PIC;
//        return super.getItemViewType(position);
    }


    static class ViewPicHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_msg)
        ImageView ivMsg;
        @BindView(R.id.chat_item_fail)
        ImageView ivChatItemFail;
        @BindView(R.id.chat_item_progress)
        ContentLoadingProgressBar chatItemProgress;

        ViewPicHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            chatItemProgress.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(view.getContext(), R.color.blue3), PorterDuff.Mode.MULTIPLY);
        }
    }

    static class ViewVideoHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_video_frame)
        ImageView ivVideoFrame;
        @BindView(R.id.iv_video_play)
        ImageView ivVideoPlay;
        @BindView(R.id.chat_item_fail)
        ImageView ivChatItemFail;
        @BindView(R.id.chat_item_progress)
        ContentLoadingProgressBar chatItemProgress;

        ViewVideoHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            chatItemProgress.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(view.getContext(), R.color.blue3), PorterDuff.Mode.MULTIPLY);
        }

    }

    private OnChatViewItemClickListener onChatViewItemClickListener;

    public void setOnChatViewItemClickListener(OnChatViewItemClickListener onChatViewItemClickListener) {
        this.onChatViewItemClickListener = onChatViewItemClickListener;
    }
}
